package com.sun.androidexerciseproject.function.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;

import com.sun.androidexerciseproject.function.receiver.MyTestReceiver;
import com.sun.androidexerciseproject.function.util.LogUtil;

public class MyTestService extends Service {
    public MyTestService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.log("onStartCommand startedId = " + startId);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        long triggerTime = SystemClock.elapsedRealtime() + 1000;
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, MyTestReceiver.class), 0);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, triggerTime, 2000, pendingIntent);
        return super.onStartCommand(intent, flags, startId);
    }
}
